﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class SalesorderS : SalesorderSI
    {
        private readonly WmssystemContext myDb;

        public SalesorderS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }
        public Task<List<PO_Salesorder>> Fye(int pageSize, int currentPage, string Number = null, string Status = null, string Cuname = null, DateTime? date = null, DateTime? date1 = null)
        {
            var query = myDb.PO_Salesorder.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Number))
            {
                query = query.Where(c => c.S_OSalesOrder.Contains(Number));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.S_ODocumentStatus == Status);
            }
            if (!string.IsNullOrEmpty(Cuname))
            {
                query = query.Where(c => c.S_OCustomerName == Cuname);
            }
            if (date != null)
            {
                query = query.Where(e => e.S_OSalesDate >= date);
            }
            if (date1 != null)
            {
                query = query.Where(e => e.S_OSalesDate <= date1);
            }

            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public Task<List<PO_Salesorder>> Qu()
        {
            return myDb.PO_Salesorder.ToListAsync();
        }
        public Task<int> Add(PO_Salesorder pO_Salesorder)
        {
            myDb.PO_Salesorder.Add(pO_Salesorder);
            return myDb.SaveChangesAsync();
        }

        public async Task<int> XsShen(PO_Salesorder pO_Salesorder, string to)
        {
            // 1. 根据指定条件查找要审核的记录
            var purchaseOrder = await myDb.PO_Salesorder.FindAsync(pO_Salesorder.S_OIndex);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }
            if (to == "通过")
            {
                purchaseOrder.S_ODocumentStatus = "未出库";
                //把销售的数量回写到入库明细表中
                var ppList = await myDb.PO_Salesorderdetail.Where(c => c.S_DSaCode == purchaseOrder.S_OSalesOrder).ToListAsync();
                foreach (var pp in ppList)
                {
                    var ccList = await myDb.PO_Inventorydetails.Where(c => c.I_IProductCode == pp.S_DProductNumber).ToListAsync();
                    foreach (var cc in ccList)
                    {
                        cc.I_IQuantity = (int?)(pp.S_DCurrentInventory - pp.S_DQuantitySold);
                        // 如果需要更新其他属性，也可以在这里进行修改
                    }
                }
            }
            else
            {
                purchaseOrder.S_ODocumentStatus = "未审核";
            }
            // 2. 更新找到的记录的相应字段
            purchaseOrder.S_OApprover = "admin"; // 以此类推，更新需要修改的字段
            purchaseOrder.S_OApproverTime = DateTime.Now;
            purchaseOrder.P_Reviewcomments = pO_Salesorder.P_Reviewcomments;


            // 3. 将更新后的记录保存回数据库中
            myDb.PO_Salesorder.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }

        public async Task<int> XsDel(List<int> Ids)
        {
            // 使用 LINQ 查询语句检索相应的记录
            var detailsToDelete = await myDb.PO_Salesorder
                .Where(d => Ids.Contains(d.S_OIndex))
                .ToListAsync();

            // 删除查询结果
            myDb.PO_Salesorder.RemoveRange(detailsToDelete);

            // 保存删除操作到数据库
            return await myDb.SaveChangesAsync();
        }

        public async Task<int> XsUpd(PO_Salesorder pO_Salesorder)
        {
            // 1. 根据指定条件查找要修改的记录
            var purchaseOrder = await myDb.PO_Salesorder.FindAsync(pO_Salesorder.S_OIndex);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }

            // 2. 更新找到的记录的相应字段
            purchaseOrder.S_OSalesDate = pO_Salesorder.S_OSalesDate; // 以此类推，更新需要修改的字段
            purchaseOrder.S_OContactPerson = pO_Salesorder.S_OContactPerson;
            purchaseOrder.S_OContactInformation = pO_Salesorder.S_OContactInformation;
            purchaseOrder.S_Reamr = pO_Salesorder.S_Reamr;

            // 3. 将更新后的记录保存回数据库中
            myDb.PO_Salesorder.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }
    }
}
